package com.imooc.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.imooc.enums.Sex;
import com.imooc.enums.ShowWhichName;
import com.imooc.enums.UserRole;
import com.imooc.exception.GraceException;
import com.imooc.feign.WorkMicroServiceFeign;
import com.imooc.mapper.UsersMapper;
import com.imooc.mq.InitResumeMQConfig;
import com.imooc.mq.InitResumeMQProducerHandler;
import com.imooc.pojo.Users;
import com.imooc.result.GraceJSONResult;
import com.imooc.result.ResponseStatusEnum;
import com.imooc.service.UsersService;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.spring.annotation.GlobalTransactional;
import io.seata.tm.api.GlobalTransactionContext;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author liansen
 * @since 2022-12-15
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Autowired
    private WorkMicroServiceFeign workMicroServiceFeign;

    @Autowired
    private InitResumeMQProducerHandler producerHandler;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //默认头像
    private static final String USER_FACE1 = "http://122.152.205.72:88/group1/M00/00/05/CpoxxF6ZUySASMbOAABBAXhjY0Y649.png";

    @Override
    public Users userIsExistsByMobile(String mobile) {
        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getMobile, mobile);
        return baseMapper.selectOne(wrapper);
    }

    @Override
//    @Transactional
    @GlobalTransactional
    public Users createUser(String mobile) {
        Users user = new Users();
        //LocalDate.now() 和 new Date()什么区别
        user.setBirthday(LocalDate.now());
        user.setCountry("中国");
        user.setDescription("这个人什么都没留下");
        user.setMobile(mobile);
        user.setFace(USER_FACE1);
        user.setNickname("用户" + mobile);
        user.setRealName("用户" + mobile);
        user.setShowWhichName(ShowWhichName.realname.type);
        user.setSex(Sex.secret.type);
        user.setPosition("程序员");
        user.setRole(UserRole.RECRUITER.type);
        user.setCreatedTime(LocalDateTime.now());
        user.setUpdatedTime(LocalDateTime.now());
        baseMapper.insert(user);
        //初始化简历 (远程调用工作简历微服务)
        GraceJSONResult result = workMicroServiceFeign.initResume(user.getId());
        String s = new Gson().toJson(result);
        System.out.println("结果:" + s);
        if(result.getStatus() != 200){
            String xid = RootContext.getXID();
            if(StringUtils.isNotBlank(xid)){
                try {
                    GlobalTransactionContext.reload(xid);
                } catch (TransactionException e) {
                    e.printStackTrace();
                }finally {
                    GraceException.display(ResponseStatusEnum.USER_REGISTER_ERROR);
                }
            }
        }
        return user;
    }


    @Transactional
    @Override
    public Users createUserAndInitResumeMQ(String mobile) {
        Users user = new Users();
        //LocalDate.now() 和 new Date()什么区别
        user.setBirthday(LocalDate.now());
        user.setCountry("中国");
        user.setDescription("这个人什么都没留下");
        user.setMobile(mobile);
        user.setFace(USER_FACE1);

        user.setNickname("用户" + mobile);
        user.setRealName("用户" + mobile);
        user.setShowWhichName(ShowWhichName.realname.type);
        user.setSex(Sex.secret.type);
        user.setPosition("程序员");
        user.setRole(UserRole.CANDIDATE.type);
        user.setCreatedTime(LocalDateTime.now());
        user.setUpdatedTime(LocalDateTime.now());
        baseMapper.insert(user);

        //通过消息助手类进行本地消息保存
        producerHandler.saveLocalMsg(InitResumeMQConfig.INIT_RESUME_EXCHANGE,
                                    InitResumeMQConfig.INIT_RESUME_ROUTINGKEY,
                                    user.getId());
        return user;
    }
}